import { message } from "@/utils/message";
import { updateCertificateStatus } from "@/api/certificate";
import { computed } from "vue";

// 需是hooks写法（函数中有return），避免失去响应性
export function useColumnsNow(
  certificateListNow: any,
  handleViewCallback?: (row: any) => void,
  handleEditCallback?: (row: any) => void,
  handleRefreshCallback?: () => void
) {
  const tableDataNow = computed(() => certificateListNow.value || []);
  const columnsNow: TableColumnList = [
    {
      label: "姓名",
      prop: "username",
      width: 120
    },
    {
      label: "性别",
      prop: "gender",
      width: 80,
      cellRenderer: ({ row }) => {
        // 支持数字和字符串类型
        const gender = row.gender;
        let genderText = "未知";
        let genderColor = "#909399";

        if (gender === 0 || gender === "0") {
          genderText = "男";
          genderColor = "#409EFF";
        } else if (gender === 1 || gender === "1") {
          genderText = "女";
          genderColor = "#E6A23C";
        }

        return (
          <span style={`color: ${genderColor}; font-weight: 500;`}>
            {genderText}
          </span>
        );
      }
    },
    {
      label: "单位",
      prop: "unit",
      width: 150
    },
    {
      label: "职务",
      prop: "job",
      width: 120
    },
    {
      label: "手机",
      prop: "telephone",
      width: 140
    },
    {
      label: "邮箱",
      prop: "email",
      width: 200
    },
    {
      label: "身份证",
      prop: "id_card",
      width: 180
    },
    {
      label: "现居住地址",
      prop: "addres1",
      width: 250
    },
    {
      label: "户籍地详址",
      prop: "addres2",
      width: 250
    },
    {
      label: "照片",
      prop: "photo",
      width: 100,
      cellRenderer: ({ row }) => {
        const photoUrl = row.photo
          ? `https://exhibition-bucket.oss-cn-beijing.aliyuncs.com${row.photo}`
          : "/placeholder-avatar.png";
        return (
          <img
            src={photoUrl}
            alt="照片"
            style="width: 50px; height: 50px; object-fit: cover; border-radius: 4px;"
          />
        );
      }
    },
    {
      label: "审批状态",
      prop: "status",
      width: 120,
      align: "center",
      cellRenderer: ({ row }) => {
        let statusText = "";
        let statusType = "";

        switch (row.status) {
          case 0:
            statusText = "审核中";
            statusType = "warning";
            break;
          case 1:
            statusText = "已通过";
            statusType = "success";
            break;
          case 2:
            statusText = "已退回";
            statusType = "danger";
            break;
          case 3:
            statusText = "待提交";
            statusType = "warning";
            break;
          default:
            statusText = "未知";
            statusType = "info";
        }

        return <el-tag type={statusType}>{statusText}</el-tag>;
      }
    },
    {
      label: "操作",
      fixed: "right",
      width: 220,
      cellRenderer: ({ index, row }) => (
        <>
          <el-button size="small" onClick={() => handleView(row)}>
            查看
          </el-button>
          <el-button size="small" onClick={() => handleEdit(index + 1, row)}>
            修改
          </el-button>
          <el-button
            size="small"
            type="danger"
            onClick={() => handleSubmit(row)}
          >
            提交审批
          </el-button>
        </>
      )
    }
  ];

  const handleView = (row: any) => {
    if (handleViewCallback) {
      handleViewCallback(row);
    } else {
      message(`您查看了数据：${JSON.stringify(row)}`, {
        type: "success"
      });
    }
  };

  const handleEdit = (index: number, row) => {
    // 统一拦截提示：0 审核中、1 已通过
    if (row.status === 0) {
      message("证件正在审核中，不能修改", { type: "warning" });
      return;
    }
    if (row.status === 1) {
      message("证件已通过，不能修改", { type: "warning" });
      return;
    }

    if (handleEditCallback) {
      handleEditCallback(row);
    } else {
      message(`您修改了第 ${index} 行，数据为：${JSON.stringify(row)}`, {
        type: "success"
      });
    }
  };

  const handleSubmit = async (row: any) => {
    // 状态：0=审核中，1=已通过，2=未通过，3=待提交
    if (row.status === 0) {
      message("已提交，不可重复审批", { type: "warning" });
      return;
    }
    if (row.status === 1) {
      message("已通过，不可重复审批", { type: "warning" });
      return;
    }
    try {
      const result: any = await updateCertificateStatus(
        row.id,
        row.exhibitor_id,
        0
      );
      if (result.code === 1) {
        message("提交审批成功", { type: "success" });
        // 刷新列表
        if (handleRefreshCallback) {
          handleRefreshCallback();
        }
      } else {
        message(result.msg || "提交审批失败", { type: "error" });
      }
    } catch (error) {
      console.error("提交审批失败:", error);
      message("提交审批失败", { type: "error" });
    }
  };

  return {
    columnsNow,
    tableDataNow
  };
}
